import { message } from 'antd'

export const ExportToExcel = async (url: string, fileName: string) => {
  message.info('正在导出...')
  fetch(url, {
    method: 'GET'
  })
    .then(response => {
      if (response.ok) {
        // 将响应转换为 blob
        return response.blob()
      } else {
        message.error('导出失败')
        throw new Error('导出失败')
      }
    })
    .then(blob => {
      // 创建一个链接并触发下载
      const url = window.URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = url
      a.download = fileName
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      window.URL.revokeObjectURL(url)
      message.success('导出成功')
    })
    .catch(error => {
      message.error('导出失败')
      console.error('导出失败:', error)
    })
}
